package com.fatesgo.config;

import com.fatesgo.exception.AuthenticationException;
import com.fatesgo.util.Response;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.lang.reflect.UndeclaredThrowableException;
@ControllerAdvice
public class GlobalExceptionHandleConfig {



    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Response handleUserNotExistException(AuthenticationException e) {
    	e.printStackTrace();
        return Response.error(e.getMessage(),null,401);
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Response handlerException(Exception e){
    	e.printStackTrace();
        //annotation抛出的异常
        if(e instanceof UndeclaredThrowableException) {
            e = (Exception) ((UndeclaredThrowableException)e).getUndeclaredThrowable();
        }
        return  Response.error(e.getMessage());
    }
}
